package com.zxl.common.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.zxl.common.exception.BusinessException;
import com.zxl.common.mapper.SysUserMapper;
import com.zxl.common.mapper.SysUserRoleMapper;
import com.zxl.common.model.bo.user.UserAddQuery;
import com.zxl.common.model.pojo.SysUser;
import com.zxl.common.model.pojo.SysUserRole;
import com.zxl.common.model.query.user.UserListQuery;
import com.zxl.common.model.vo.user.UserListVo;
import com.zxl.common.service.SysUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zxw
 * @since 2023-03-30
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;


    @Override
    public SysUser validUserCode(String userName) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",userName);
        SysUser sysUser = getOne(queryWrapper);
        return sysUser;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void doSave(UserAddQuery userAdd, Integer roleId) {
        SysUser sysUser = new SysUser();
        BeanUtil.copyProperties(userAdd,sysUser);
        String password = sysUser.getPassword();
        // 密码加盐 hash
        String salt = RandomUtil.randomString(32);
        password = DigestUtil.sha256Hex(salt + password);
        sysUser.setSalt(salt);
        sysUser.setPassword(password);
        sysUserService.save(sysUser);
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setUserId(sysUser.getId());
        sysUserRole.setRoleId(roleId);
        sysUserRoleMapper.insert(sysUserRole);
    }

    @Override
    public boolean validPasswd(String username, String password) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",username);
        queryWrapper.eq("password",password);
        SysUser sysUser = getOne(queryWrapper);
        if(sysUser!=null){
            return true;
        }
        return false;
    }

    @Override
    public List<UserListVo> selectUserList(UserListQuery userListQuery) {
        PageHelper.startPage(userListQuery.getPageNumber(),userListQuery.getPageSize());
        List<UserListVo> userListVos = sysUserMapper.selectUserList(userListQuery);
        return userListVos;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Integer id) {
        sysUserMapper.deleteById(id);
        sysUserRoleMapper.delete(new QueryWrapper<SysUserRole>()
                .eq("user_id", id));
    }

    @Override
    public void updateUser(UserAddQuery userAdd) {
        sysUserRoleMapper.update(null, new UpdateWrapper<SysUserRole>()
                .eq("user_id",userAdd.getId())
                .set("role_id", userAdd.getRoleId()));
    }

    @Override
    public SysUser getInfo(Integer id) {
        SysUser sysUser = sysUserMapper.selectOne(new QueryWrapper<SysUser>()
                .eq("id", id)
                .select("id", "nick_name"));
        return sysUser;
    }
}
